/**
 * 
 */
package org.mspring.mlog.web.module.ucenter;

import org.apache.commons.lang3.StringUtils;
import org.mspring.mlog.core.ServiceFactory;
import org.mspring.mlog.entity.security.User;
import org.mspring.mlog.web.module.AbstractWidget;
import org.springframework.ui.Model;

/**
 * @author Gao Youbo
 * @since 2013-6-7
 * @description
 * @TODO
 */
public class AbstractUCenterWidget extends AbstractWidget {

    protected User spaceUser;

    /**
     * 当前访问页的所有者
     */
    public static final String SPACE_USER = "spaceUser";
    

    /**
     * 设置当前访问页的所有者
     * 
     * @param user
     * @param model
     */
    public void setSpaceUser(User user, Model model) {
        this.spaceUser = user;
        model.addAttribute(SPACE_USER, user);
    }

    public User getSpaceUser(String username, Model model) {
        User user = getSpaceUser(username);
        if (user != null) {
            setSpaceUser(user, model);
        }
        return user;
    }

    @Deprecated
    public User getSpaceUser(String username) {
        if (StringUtils.isBlank(username)) {
            spaceUser = null;
            return spaceUser;
        }
        User user = ServiceFactory.getUserService().getUserByName(username);
        if (user != null) {
            spaceUser = user;
        } else {
            spaceUser = null;
        }
        return spaceUser;
    }
}
